package com.gjr.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gjr.eduservice.entity.EduSubject;
import com.gjr.eduservice.entity.excel.SubjectData;
import com.gjr.eduservice.service.EduSubjectService;
import com.gjr.servicebase.exceptionhandler.GuliException;
import lombok.Data;
import org.springframework.util.ObjectUtils;

/**
 * 监听器，不能交由 Spring 进行管理（Bean），只能通过New方式使用
 */
@Data
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {
    private EduSubjectService eduSubjectService;

    /**
     * 读取excel内容，一行一行进行读取
     * <p>
     * table   edu_subject
     *
     * @param subjectData 读取的对象
     */
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if (ObjectUtils.isEmpty(subjectData)) {
            throw new GuliException(20001, "文件数据为空");
        }

        /*  Excel数据格式
                一级分类           二级分类
                前端开发           vue

            一行一行读取，每次读取有两个值，第一个值一级分类，第二个值二级分类*/
        if (!ObjectUtils.isEmpty(subjectData)) {
            EduSubject existOneSubject = this.existOneSubject(eduSubjectService, subjectData.getOneSubjectName());
            // 为空：没有相同一级分类
            if (ObjectUtils.isEmpty(existOneSubject)) {
                existOneSubject = new EduSubject();
                existOneSubject.setParentId("0");
                existOneSubject.setTitle(subjectData.getOneSubjectName());

                if (!eduSubjectService.save(existOneSubject)) {
                    throw new GuliException(20001, "一级菜单添加失败");
                }
            }

            // 添加二级分类
            // 获取以及分类id值
            String parentId = existOneSubject.getId();
            EduSubject existTwoSubject = this.existTwoSubject(eduSubjectService, subjectData.getTwoSubjectName(), parentId);
            // 判断二级分类是否重复
            if (ObjectUtils.isEmpty(existTwoSubject)) {
                existTwoSubject = new EduSubject();
                existTwoSubject.setParentId(parentId);
                existTwoSubject.setTitle(subjectData.getTwoSubjectName());

                if (!eduSubjectService.save(existTwoSubject)) {
                    throw new GuliException(20001, "一级菜单添加失败");
                }
            }
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    /**
     * 判断一级分类不能重复添加
     *
     * @param title 课程名称
     * @return 课程科目实体
     */
    private EduSubject existOneSubject(EduSubjectService eduSubjectService, String title) {
        LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduSubject::getTitle, title)
                .eq(EduSubject::getParentId, "0");

        EduSubject oneEduSubject = eduSubjectService.getOne(wrapper, false);

        return oneEduSubject;
    }

    /**
     * 判断二级分类不能重复添加
     *
     * @param title    课程名称
     * @param parentId 父级id
     * @return 课程科目实体
     */
    private EduSubject existTwoSubject(EduSubjectService eduSubjectService, String title, String parentId) {
        LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduSubject::getTitle, title)
                .eq(EduSubject::getParentId, parentId);

        EduSubject twoEduSubject = eduSubjectService.getOne(wrapper, false);

        return twoEduSubject;
    }

    public SubjectExcelListener() {
    }

    public SubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }
}
